const Controller = require("uni-cloud-router").Controller;
const {validateToken} = require('validdate-token');
module.exports = class UserController extends Controller{
	/**
	 * @param {Object} token
	 * 验证token
	 */
	async _checkToken(token){
		let tokenRes;
		try{
			tokenRes = await validateToken(token);
			if(tokenRes.code !== 0){
				return{
					status:-1,
					msg:"稍后重试",
					errCode:tokenRes.errCode
				}
			} else {
				return {
					status:0,
					msg:"token有效",
				}
			}
		}catch(e){
			return {
			  status: -1,
			  errCode: 'TOKEN_INVALID',
			  msg: 'token无效'
			}
		}
	}
	/**
	 * 2020-11-22
	 * 员工获取项目 | 进行中 | 历史项目
	 */
	async getUnderwayProject(){
		let {token,wkID,projectStatus} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.wkProjectAdmin.getUnderwayProject({wkID,projectStatus});
		}
	}
	/**
	 * 2020-11-24
	 * 员工根据项目ID获取 项目工作信息
	 */
	async getWorkerInfo(){
		let {token,projectID,wkID,pageNo,pageSize} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.wkProjectAdmin.getWorkerInfo({projectID,wkID,pageNo,pageSize});
		}
	}
	/**
	 * 2020-12-27
	 * 获取员工被当前项目踢出的信息
	 */
	async getWKhistoryProject(){
		let {token,id,type} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.wkProjectAdmin.getWKhistoryProject({id,type});
		}
	}
}